﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OpenQuarters.WebQuarters.Core
{
    public partial class Role : CMSEntity<Role, string>
    {
        public override System.Linq.Expressions.Expression<Func<Role, bool>> GetIDSelector(string ID)
        {
            return e => e.RoleId == ID;
        }

        public override string GetKey(Role Entity)
        {
            return Entity.RoleId;
        }

        public override bool IsNew(Role Entity)
        {
            return Table.FirstOrDefault(e => e.RoleId == Entity.RoleId) == null;
        }

        public override string ToString()
        {
            return this.RoleId + " - " + this.Name;
        }

        public const string GuestRoleId = "Guest";
        public const string UserRoleId = "User";
        public const string EditorRoleId = "Editor";
        public const string AdminRoleId = "Admin";
        public const string SuperAdminRoleId = "SuperAdmin";

        public static Role Default
        {
            get
            {
                var r = Table.FirstOrDefault(e => e.IsDefault);
                if (r == null)
                {
                    r = new Role()
                    {
                        IsDefault = true,
                        Name = "Guest",
                        RoleId = GuestRoleId
                    }.Save();

                    var sa = SuperAdmin;
                    var a = Admin;
                    var e = Editor;
                    var u = User;
                }
                return r;
            }
        }

        public static Role User
        {
            get
            {
                var r = Table.FirstOrDefault(e => e.RoleId == UserRoleId);
                if (r == null)
                {
                    r = new Role()
                    {
                        IsDefault = false,
                        Name = "User",
                        RoleId = UserRoleId
                    }.Save();
                }
                return r;
            }
        }

        public static Role Editor
        {
            get
            {
                var r = Table.FirstOrDefault(e => e.RoleId == EditorRoleId);
                if (r == null)
                {
                    r = new Role()
                    {
                        IsDefault = false,
                        Name = "Editor",
                        RoleId = EditorRoleId
                    }.Save();
                }
                return r;
            }
        }

        public static Role Admin
        {
            get
            {
                var r = Table.FirstOrDefault(e => e.RoleId == AdminRoleId);
                if (r == null)
                {
                    r = new Role()
                    {
                        IsDefault = false,
                        Name = "Admin",
                        RoleId = AdminRoleId
                    }.Save();
                }
                return r;
            }
        }

        public static Role SuperAdmin
        {
            get
            {
                var r = Table.FirstOrDefault(e => e.RoleId == SuperAdminRoleId);
                if (r == null)
                {
                    r = new Role()
                    {
                        IsDefault = false,
                        Name = "Super Admin",
                        RoleId = SuperAdminRoleId
                    }.Save();
                }
                return r;
            }
        }

        public static void BasicSetup()
        {
            var r = Role.Default;
        }


    }
}
